//
//  XZBindPhoneNumVC.m
//  Teecloud
//
//  Created by PC on 2018/10/22.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "XZBindPhoneNumVC.h"
#import "JKCountDownButton.h"
@interface XZBindPhoneNumVC ()<UITextFieldDelegate>
//手机号输入框
@property (weak, nonatomic) IBOutlet UITextField *phoneTf;
//验证码输入框
@property (weak, nonatomic) IBOutlet UITextField *messageCodeTf;
//获取验证码按钮
@property (weak, nonatomic) IBOutlet JKCountDownButton *messageBtn;


@end

@implementation XZBindPhoneNumVC
#pragma mark - LifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"绑定手机号";
    self.view.backgroundColor =  kWhiteColor;
   
}
#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{//限制手机号最多只能输入11位
    if (textField == self.phoneTf) {
        if ([string isEqualToString:@""]) {
            return YES;
        }else if (self.phoneTf.text.length >= 11) {
            self.phoneTf.text = [textField.text substringToIndex:11];
            return NO;
        }
    }
    return YES;
}


//获取短信验证码
- (IBAction)getMessageCodeButtonAction:(JKCountDownButton *)sender {
    if (kIsEmptyString(self.phoneTf.text)) {
        [MBProgressHUD showErrorOrSuc:@"请输入手机号" toView:self.view];
        return;
    }
    if (![self.phoneTf.text zj_isTrueMobilePhone]) {
        [MBProgressHUD showErrorOrSuc:@"请输入正确的手机号" toView:self.view];
        return;
    }
    
    kWeakObject(self)
    
    NSDictionary *dict= @{
                          @"from":FROM_IOS,
                          @"busiOrgId":[XGMArchiverTools cacheForKey:BUSIORGID],
                          @"loginFinger":[XGMArchiverTools cacheForKey:USER_identifierNumber],
                          @"userId":[XGMArchiverTools cacheForKey:USER_UserId],
                          @"mobileCode":self.phoneTf.text,
                          @"temp":@"3"
                          };
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HJNetwork POSTWithURL:[NSString stringWithFormat:@"/do-api.ashx?$m=SendVerifyCode&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {
        [MBProgressHUD hideHUDForView:weakself.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                NSLog(@"%@",responseObject);
                [MBProgressHUD showErrorOrSuc:@"验证码发送成功" toView:weakself.view];
                //验证码倒计时
                sender.enabled = NO;
                [sender startCountDownWithSecond:60];
                sender.backgroundColor =  kDarkGrayColor;
                [sender countDownChanging:^NSString *(JKCountDownButton *countDownButton,NSUInteger second) {
                    NSString *title = [NSString stringWithFormat:@"%zd秒后重新获取验证码",second];
                    return title;
                }];
                [sender countDownFinished:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
                    countDownButton.backgroundColor = kGreenColor;
                    countDownButton.enabled = YES;
                    return @"获取短信验证码";
                }];
                
            }else{
                /**显示错误信息*/
                [MBProgressHUD showErrorOrSuc:[responseObject objectForKey:RESULT_NAME_Message] toView:weakself.view];
            }
        }else{
            [MBProgressHUD showErrorOrSuc:[NSString stringWithFormat:@"%@",error] toView:weakself.view];
        }
    }];
}
//确认绑定
- (IBAction)confirmBindPhoneButtonAction:(UIButton *)sender {
    
    if (kIsEmptyString(self.phoneTf.text)) {
        [MBProgressHUD showErrorOrSuc:@"请输入手机号" toView:self.view];
        return;
    }
    if (![self.phoneTf.text zj_isTrueMobilePhone]) {
        [MBProgressHUD showErrorOrSuc:@"请输入正确的手机号" toView:self.view];
        return;
    }
    if (kIsEmptyString(self.messageCodeTf.text)) {
        [MBProgressHUD showErrorOrSuc:@"请输入验证码" toView:self.view];
        return;
    }
    
    if (self.messageCodeTf.text.length != 6) {
        [MBProgressHUD showErrorOrSuc:@"验证码的位数为6位" toView:self.view];
        return;
    }
    [self updateUserBindPhoneNum];
}

//更新用户绑定的手机号
- (void)updateUserBindPhoneNum{
    NSDictionary *dict= @{
                          @"from":FROM_IOS,
                          @"busiOrgId":[XGMArchiverTools cacheForKey:BUSIORGID],
                          @"loginFinger":[XGMArchiverTools cacheForKey:USER_identifierNumber],
                          @"userId":[XGMArchiverTools cacheForKey:USER_UserId],
                          @"fieldName":@"MobileCode",
                          @"fieldValue":self.phoneTf.text,
                          @"verifyCode":self.messageCodeTf.text
                          };
    
    __weak typeof(self)weakSelf = self;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HJNetwork POSTWithURL:[NSString stringWithFormat:@"/ajax-api.ashx?$c=TeeyunApp&$m=UpdateUserInfo&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                //绑定成功 返回
                [MBProgressHUD showErrorOrSuc:@"绑定成功" toView:weakSelf.view];
                [weakSelf.navigationController popViewControllerAnimated:true];
            }else{
                /**显示错误信息*/
                [MBProgressHUD showErrorOrSuc:[responseObject objectForKey:RESULT_NAME_Message] toView:weakSelf.view];
            }
            
        }else{
            XZLog(@"%@",error);
        }
    }];
}
@end
